
package cell

import (
	"github.com/treelab/updatecolumn/models"
)

// InstanceType of cell
func InstanceType() int32 {
	return models.RollUp
}

  setCellValue({ cell, action }: { cell: Cell; action: UpdateCellAction }) {
    // remove escapeJS value, rollup column value had escapeJS
    const value = (action.value as FormulaAndRollupCellValue).result?.toString();
    cell.result = value;
    return this;
  }

  async getRowData(command: UpdateColumn_v2) {
    const { column } = command;
    const referenceColumn = column.hasRecordReferenceColumn.get()[0];
    const table = referenceColumn?.fromTable.get();
    let referenceColumnId = referenceColumn?.uid;
    if (!referenceColumnId) {
      referenceColumnId = (command.columnConfig as RollupColumnConfig).recordReferenceColumnId;
    }
    let tableId: string;
    if (table?.length) {
      tableId = table[0].uid!;
    } else {
      const rfColumn = await this.helperReadService.getNodeById(referenceColumnId!, [
        `fromTable:${Relation.FROM_TABLE} {uid}`,
      ]);
      if (rfColumn) {
        tableId = rfColumn.fromTable[0].uid;
      }
    }
    if (tableId!) {
      return await this.rowReadService.rowForFormula_v2(command.coreId, tableId!, referenceColumnId!);
    }
    return [];
  }

